package com.efeichong.oss;

import com.efeichong.exception.BaseException;
import lombok.RequiredArgsConstructor;

/**
 * @author lxk
 * @date 2020/11/5
 * @description oss工厂类 目前仅支持阿里云/腾讯云/金山云/本地/亚马逊
 */
@RequiredArgsConstructor
public class OssFactory {

    private final OssConfiguration ossConfiguration;

    /**
     * 通过工厂创建文件oss类,默认使用本地
     *
     * @param ossType {@link OssType}
     * @return
     */
    public <T> T create(OssType ossType) {
        OssConfigLocal ossConfigLocal = ossConfiguration.getLocal();
        OssConfigS3 ossConfigS3 = ossConfiguration.getS3();
        if (ossType == null || ossType.equals(OssType.LOCAL)) {
            //本地
            if (ossConfigLocal == null) {
                throw new BaseException("本地文件操作时:缺少配置oss.local");
            }
            if (StringUtils.isBlank(ossConfigLocal.getBaseDir())) {
                throw new BaseException("本地文件操作时:根路径 baseDir不能为空");
            }
            if (StringUtils.isBlank(ossConfigLocal.getViewUrlPrefix())) {
                throw new BaseException("本地文件操作时:展示路径 viewUrlPrefix不能为空");
            }
            if (StringUtils.isBlank(ossConfigLocal.getShowDomainUrl())) {
                throw new BaseException("本地文件操作时:域名 showDomainUrl不能为空");
            }
        } else {
            //s3
            if (ossConfigS3 == null) {
                throw new BaseException("s3文件操作时:缺少配置oss.s3");
            }
            if (StringUtils.isBlank(ossConfigS3.getAccessKeyId())) {
                throw new BaseException("s3文件操作时:访问用户id accessKeyId不能为空");
            }
            if (StringUtils.isBlank(ossConfigS3.getAccessSecret())) {
                throw new BaseException("s3文件操作时:访问密钥 accessSecret不能为空");
            }
            if (StringUtils.isBlank(ossConfigS3.getEndPoint())) {
                throw new BaseException("s3文件操作时:对外服务的访问域名 endPoint不能为空");
            }
            if (StringUtils.isBlank(ossConfigS3.getRegion())) {
                throw new BaseException("s3文件操作时:区域 region不能为空");
            }
        }


        switch (ossType) {
            case JS:
                return (T) new JsOssService(ossConfigS3);
            case ALY:
                return (T) new AlyOssService(ossConfigS3);
            case AMAZON:
                return (T) new AmazonOssService(ossConfigS3);
            case TX:
                return (T) new TxOssService(ossConfigS3);
            default:
                //默认走本地
                return (T) new LocalOssService(ossConfigLocal);
        }
    }


    /**
     * 使用本地
     *
     * @return
     */
    public OssService create() {
        String enabled = ossConfiguration.getEnabled();
        if (StringUtils.isBlank(enabled)) {
            throw new BaseException("未配置oss.enabled");
        }
        if (OssType.JS.name().equalsIgnoreCase(enabled)) {
            return create(OssType.JS);
        }
        if (OssType.ALY.name().equalsIgnoreCase(enabled)) {
            return create(OssType.ALY);
        }
        if (OssType.AMAZON.name().equalsIgnoreCase(enabled)) {
            return create(OssType.AMAZON);
        }
        if (OssType.TX.name().equalsIgnoreCase(enabled)) {
            return create(OssType.TX);
        }
        return create(OssType.LOCAL);
    }
}
